#include "CCoroutineGroup.h"
#include "CCoroutineGroupImpl.h"

namespace TB
{
	CCoroutineGroup::CCoroutineGroup(UINT32 dwCoroutineCount, const std::weak_ptr<CThreadPool>& poThreadPool)
	{
		m_poImpl = std::make_shared<CCoroutineGroupImpl>(poThreadPool);
		m_poImpl->m_poSelf = m_poImpl;
		m_poImpl->m_dwSize = dwCoroutineCount;
		m_poImpl->m_oIdleCount = dwCoroutineCount;
		m_oCoroutines.reserve(dwCoroutineCount);
		for (UINT32 i = 0; i != dwCoroutineCount; ++i)
		{
			std::shared_ptr<CCoroutine> poCoroutine(new CCoroutine(this, m_poImpl, m_poImpl->m_poThreadPool, false));
			poCoroutine->Init(poCoroutine);
			auto&& poItem = m_oCoroutines.emplace_back(poCoroutine);
			m_poImpl->m_oIdleCoroutineQueue.Push(poItem->m_poImpl);
		}
	}

	CCoroutineGroup::~CCoroutineGroup()
	{
	}

	void CCoroutineGroup::_Post(const std::function<void()>& rcbFunction)
	{
		m_poImpl->Post(rcbFunction);
	}

	const std::weak_ptr<CThreadPool>& CCoroutineGroup::GetThreadPool() const
	{
		return m_poImpl->m_poThreadPool;
	}

	std::weak_ptr<CThreadPool>& CCoroutineGroup::GetThreadPool()
	{
		return m_poImpl->m_poThreadPool;
	}
}